<?php
include_once('class/produk.php');
include_once('class/carosel.php');

$produk_hot=new Produk();
$carosel=new Carosel();

$caro=$carosel->getCarosel();
$data_tampil=$produk_hot->getHotProduk();

$data_new=$produk_hot->getNewProduk();

if(isset($_GET['admin']) && isset($_GET['id']))
{
	$id=$_GET['id'];
	if(isset($_SESSION['login']) && $_SESSION['level']==1)
	{
		$del=$carosel->delCarosel($id);
		if($del==1)
		{
			echo "<div class=\"alert\">
				<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>
				<strong>Peringatan!</strong> Carosel Berhasil Di Hapus.
				</div>";
				header ('refresh: 3; carosel.php?admin');
		}
		else
		{
			echo "<div class=\"alert\">
				<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>
				<strong>Peringatan!</strong> Gagal hapus Carosel.
				</div>";
				header ('refresh: 3; carosel.php?admin');
		}
	}
	else
	{
		echo "<div class=\"alert\">
			<button type=\"button\" class=\"close\" data-dismiss=\"alert\">&times;</button>
			<strong>Peringatan!</strong> Anda Tidak Memiliki Wewenang.
			</div>";
	}
}


if(isset($_POST['buat_carosel']))
{
	$id_produk=$_POST['id_produk'];
	$judul=$_POST['judul_caro'];
	$deskripsi=$_POST['deskripsi_caro'];
	$size=$_POST['MAX_FILE_SIZE'];
	
	$fileName = $_FILES['gambar_caro']['name'];
	$tmpName = $_FILES['gambar_caro']['tmp_name'];
	$fileSize = $_FILES['gambar_caro']['size'];
	$fileType = $_FILES['gambar_caro']['type'];
	
	//$ext=explode('.',$fileName);
	$ext=$carosel->getExtension($tmpName);
	//echo $fileName;
	//echo strcmp("jpg",$ext);
	
	if(empty($judul) || empty($deskripsi) || empty($id_produk))
	{
		$error_caro="Tidak boleh Kosong";
	}
	elseif(empty($fileName))
	{
		$error_caro="upload File gambar";
	}
	elseif(!strcmp("jpg",$ext) || !strcmp("jpeg",$ext) || !strcmp("png",$ext))
	{
		$error_caro="upload File JPG atau PNG saja";
	}
	elseif($fileSize>$size)
	{
		$error_caro="File terlalu besar";
	}
	//elseif($fileType!="image/jpeg" || $fileType!="image/png" || $fileType!="image/gif")
	//{
	//	$error_caro="Tipe file salah (hanya jpeg,png atau gif)".$fileType;
	//}
	else
	{
		
		$fp = fopen($tmpName, 'r');
		$gambar = fread($fp, filesize($tmpName));
		$gambar = addslashes($gambar);
		fclose($fp);
									
		$insert=$carosel->insertCarosel($judul,$deskripsi,$id_produk, $gambar);
		if($insert==1)
		{
			$success_caro="Carosel Anda sudah dimasukkan ke katalog";
		}
		else
		{
			$error_caro="Carosel gagal dimasukkan ke katalog";
		}
	}
}

?>